home *** CD-ROM | disk | FTP | other *** search
- Path: pop.gnn.com!HoangTQ
- From: Tuyen Hoang <HoangTQ@gnn.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Need Help With CSC Homework
- Date: Sat, 16 Mar 1996 22:33:58
- Organization: GNN
- Message-ID: <4ig14c$h3p@news-e2c.gnn.com>
- References: <4g0qun$jp@news.nevada.edu>
- NNTP-Posting-Host: www-29-26.gnn.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset="us-ascii"
- X-GNN-NewsServer-Posting-Date: 17 Mar 1996 03:32:28 GMT
- X-Mailer: GNNmessenger 1.2
-
-
- > Troy code:
- >pfun.cpp
- >
- >//*************************************************
- >//** ROMANFUN.CPP **
- >//*************************************************
- >//** This program will add, subtract, multiply, **
- >//** and divide roman numbers. The numbers are **
- >//** assumed to be less than 20 characters long. **
- >//** The roman numbers should not be inputed in **
- >//** subtraction form. Examples - IV=6 IIII=4 **
- >//*************************************************
- >#include <iostream.h> //for cin and cout
- >#include "p.h" //has typedef and prototypes
- >istream& operator>>(istream& in,Romantype& x)
- >{
- >in>>x.Roman;
- >return in;
- >}
- >
- >ostream& operator<<(ostream& out,Romantype x)
- >{
- >out<<x.Roman;
- >return out;
- >}
- >
- >Romantype::Romantype()
- >{
- >int i;
- >for(i=1;i<MAXLENGTH;i++)
- >{
- >Roman[i]=' ';
- >}
- >}
- >
- >//*************************************************
- >//** ADD FUNCTION **
- >//*************************************************
- >//** This function adds two roman numbers. It **
- >//** requires a character array and two roman **
- >//** numbers. **
- >//*************************************************
- >void Romantype::operator+(Romantype numbertwo)
- >{
- >int numbero;
- >int numbert;
- >int answer;
- >Romantype answerr;
- >numbero=RomanToDecimal(Roman);
- >numbert=RomanToDecimal(numbertwo.Roman);
- >answer=numbero+numbert;
- >DecimalToRoman(answerr.Roman,answer);
- >cout<<answerr.Roman;
- >}
- >
- >//*************************************************
- >//** ROMAN TO DECIMAL FUNCTION **
- >//*************************************************
- >//** This function converts a roman number to a **
- >//** decimal number. It requires the character **
- >//** array and the number of characters in the **
- >//** array. **
- >//*************************************************
- >int Romantype::RomanToDecimal(char[MAXLENGTH])
- >{
- >int number=0; //value of number intialized to zero
- >int i; //used in for loop
- >for(i=1;i<MAXLENGTH;i++) //until end of array add values of letters
- >{
- >switch(Roman[i])
- >{
- >case 'I':number=number+1;break;
- >case 'V':number=number+5;break;
- >case 'X':number=number+10;break;
- >case 'L':number=number+50;break;
- >case 'C':number=number+100;break;
- >case 'D':number=number+500;break;
- >case 'M':number=number+1000;break;
- >}
- >}
- >return number; //return decimal value to where the function was called
- >}
- >
- >//*************************************************
- >//** DECIMAL TO ROMAN FUNCTION **
- >//*************************************************
- >//** This function converts a decimal number to **
- >//** a roman number. It requires a character **
- >//** array and the value of the decimal number. **
- >//*************************************************
- >void Romantype::DecimalToRoman(char[MAXLENGTH]/* ??? */,int answer)
- // ^^^what is the name of param. ?
- //are you sure what you want to do?
- //without the param.'s name the char array you pass in will be *thrown away*.
-
- >{
- >int i=1; //used in for loop
- >while(answer-1000>=0) //convert to roman by storing letters in array
- >{
- >answerr.Roman[i]='M';
- ^^^^ where does the 'answerr'-RomanType object- come from?
- // Do you mean you pass the 'answerr.Roman' in the function call
- //then you can use it here?
-
- >answer=answer-1000;
- >i++;
- >}
- >while(answer-500>=0)
- >{
- >answerr.Roman[i]='D';
- ^^^
- >answer=answer-500;
- >i++;
- >}
- >while(answer-100>=0)
- >{
- >answerr.Roman[i]='C';
- ^^^
- >answer=answer-100;
- >i++;
- >}
- >while(answer-50>=0)
- >{
- >answerr.Roman[i]='L';
- ^^^
- >answer=answer-50;
- >i++;
- >}
- >while(answer-10>=0)
- >{
- >answerr.Roman[i]='X';
- ^^^
- >answer=answer-10;
- >i++;
- >}
- >while(answer-5>=0)
- >{
- >answerr.Roman[i]='V';
- ^^^
- >answer=answer-5;
- >i++;
- >}
- >while(answer-1>=0)
- >{
- >answerr.Roman[i]='I';
- ^^^
- >answer=answer-1;
- >i++;
- >}
- >}
- >
-
- /* There are some important points
-
- 1. none static member function has a hidden *this* pointer. That is
- this->Roman[i] .... so and so ...( not recommented )
- or just
- Roman[i] .... so and so ... ( better )
- then you do not need passing a char[] to access the Roman char array
-
- 2. Passing a variable in a function call *does not* mean you can use
- that variable's name in the function definition body.
- i.e.
- you call the function and pass 'answerr.Roman' as a parameter
- DecimalToRoman(answerr.Roman,answer);
- and then you want to use 'answerr.Roman' name in side the function body
- this is a BIG MISTAKE
-
- 3. You obmiss the parameter name in the function definition. Is that
- your intense not to use that parameter? If I'm right, you *must* give it a
- name and then use that name in side the function definition instead of
- 'answerr.Roman' or better yet throw it away and get familiar with 'this*'-see #1.
-
- //Please review on your note or textbook
- function prototype and function definition, they have
- different meaning and different syntax
-
- function calling and parameters passing
- member function and this pointer .
- */
- Hoang Tuyen Q.
- HoangTQ@gnn.com
-
-